package com.machinecontroller.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.easivend.evprotocol.EVprotocol;
import com.google.gson.Gson;
import com.machinecontroller.R;
import com.machinecontroller.constant.McConfig;
import com.machinecontroller.model.EVModel;

import java.util.List;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 输出结果
     */
    private TextView mTvResult;
    /**
     * 串口注册
     */
    private Button mBtnRegist;
    /**
     * 串口释放
     */
    private Button mBtnClose;
    /**
     * 格子柜开门
     */
    private Button mBtnOpenDoor;
    /**
     * 格子柜门状态
     */
    private Button mBtnDoorState;
    /**
     * 格子柜照明
     */
    private Button mBtnLighting;
    /**
     * 格子柜除臭
     */
    private Button mBtnDeodorize;
    /**
     * 格子柜制冷
     */
    private Button mBtnAddCool;
    /**
     * 格子柜加热
     */
    private Button mBtnAddHot;
    /**
     * 格子柜查询
     */
    private Button mBtnSearch;
    /**
     * 综合柜出货
     */
    private Button mBtnCompositeShipment;

    private final String portName = McConfig.MC_PORT_NUM;
    private int port_id = 0;//默认，未注册
    /**
     * 格子柜柜号
     */
    private EditText mEtAddr;
    /**
     * 格子柜格子号
     */
    private EditText mEtBox;
    private boolean isSupportCool;//是否支持制冷
    private boolean isSupportHot;//是否支持加热
    private boolean isSupportLight;//是否支持照明
    private List<EVModel.EV_json.SumModel> boxSum;//箱道总数
    private boolean isOpen = true;//是否开启开管，对应opt
    private boolean isOpenDoor;//是否已开门
    private boolean isOutGoods = true;//是否出货
    private int m_type = 1;//货柜类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();

    }

    private void initView() {
        mTvResult = (TextView) findViewById(R.id.tv_result);
        mBtnRegist = (Button) findViewById(R.id.btn_regist);
        mBtnRegist.setOnClickListener(this);
        mBtnClose = (Button) findViewById(R.id.btn_close);
        mBtnClose.setOnClickListener(this);
        mBtnOpenDoor = (Button) findViewById(R.id.btn_open_door);
        mBtnOpenDoor.setOnClickListener(this);
        mBtnDoorState = (Button) findViewById(R.id.btn_door_state);
        mBtnDoorState.setOnClickListener(this);
        mBtnLighting = (Button) findViewById(R.id.btn_lighting);
        mBtnLighting.setOnClickListener(this);
        mBtnDeodorize = (Button) findViewById(R.id.btn_deodorize);
        mBtnDeodorize.setOnClickListener(this);
        mBtnAddCool = (Button) findViewById(R.id.btn_add_cool);
        mBtnAddCool.setOnClickListener(this);
        mBtnAddHot = (Button) findViewById(R.id.btn_add_hot);
        mBtnAddHot.setOnClickListener(this);
        mBtnSearch = (Button) findViewById(R.id.btn_search);
        mBtnSearch.setOnClickListener(this);
        mBtnCompositeShipment = (Button) findViewById(R.id.btn_composite_shipment);
        mBtnCompositeShipment.setOnClickListener(this);
        mEtAddr = (EditText) findViewById(R.id.et_addr);
        mEtBox = (EditText) findViewById(R.id.et_box);
    }

    @Override
    public void onClick(View v) {
        String json = null;
        EVModel mParentModel = null;
        EVModel.EV_json mModel = null;
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_regist:
                json = EVprotocol.EVPortRegister(portName);
                showToast(json);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                showToast(mModel.toString());
                if (mModel.getPort_id() > 0) {
                    port_id = mModel.getPort_id();
                    showToast("串口注册成功");
                } else {
                    showToast("串口注册失败");
                }
                break;
            case R.id.btn_close:
                checkPortId();
                json = EVprotocol.EVPortRelease(portName);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getResult() == 1) {
                    showToast("串口释放成功");
                } else {
                    showToast("串口释放失败");
                }
                break;
            case R.id.btn_open_door:
                checkPortId();
                json = EVprotocol.EVBentoOpen(port_id, Integer.parseInt(mEtAddr.getText().toString()), Integer.parseInt(mEtBox.getText().toString()));
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getResult() == 1) {
                    showToast("开门成功");
                } else {
                    showToast("开门失败");
                }
                break;
            case R.id.btn_door_state:
                checkPortId();
                json = EVprotocol.EVBentoDoorStatus(port_id, Integer.parseInt(mEtAddr.getText().toString()));
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    isOpenDoor = mModel.getResult() == 0;
                    showToast("获取格子柜门状态成功");
                } else {
                    showToast("获取格子柜门状态失败");
                }
                break;
            case R.id.btn_lighting:
                checkPortId();
                if (!isSupportLight) {
                    showToast("格子柜不支持照明，请点击查询");
                    return;
                }
                json = EVprotocol.EVBentoLight(port_id, Integer.parseInt(mEtAddr.getText().toString()), isOpen ? 1 : 0);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    if (mModel.getResult() == 0) {
                        showToast("格子柜照明成功");
                        break;
                    }
                }
                showToast("格子柜照明失败");
                break;
            case R.id.btn_deodorize:
                checkPortId();
                json = EVprotocol.EVBentoOzonectrl(port_id, Integer.parseInt(mEtAddr.getText().toString()), isOpen ? 1 : 0);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    isOpenDoor = mModel.getResult() == 1;
                    showToast("格子柜除臭通信成功");
                } else {
                    showToast("格子柜除臭通信失败");
                }
                break;
            case R.id.btn_add_cool:
                checkPortId();
                if (!isSupportCool) {
                    showToast("格子柜不支持制冷，请点击查询");
                    return;
                }
                json = EVprotocol.EVBentoCool(port_id, Integer.parseInt(mEtAddr.getText().toString()), isOpen ? 1 : 0);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    isOpenDoor = mModel.getResult() == 1;
                    showToast("格子柜制冷通信成功");
                } else {
                    showToast("格子柜制冷通信失败");
                }
                break;
            case R.id.btn_add_hot:
                checkPortId();
                if (!isSupportHot) {
                    showToast("格子柜不支持加热，请点击查询");
                    return;
                }
                json = EVprotocol.EVBentoHot(port_id, Integer.parseInt(mEtAddr.getText().toString()), isOpen ? 1 : 0);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    isOpenDoor = mModel.getResult() == 1;
                    showToast("格子柜加热通信成功");
                } else {
                    showToast("格子柜加热通信失败");
                }
                break;
            case R.id.btn_search:
                checkPortId();
                json = EVprotocol.EVBentoCheck(port_id, Integer.parseInt(mEtAddr.getText().toString()));
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    isSupportCool = mModel.getCool() == 1;
                    isSupportHot = mModel.getHot() == 1;
                    isSupportLight = mModel.getLight() == 1;
                    boxSum = mModel.getSum();
                    showToast("格子柜查询成功");
                } else {
                    showToast("格子柜查询失败");
                }
                break;
            case R.id.btn_composite_shipment:
                checkPortId();
                json = EVprotocol.EVtrade(port_id, m_type, Integer.parseInt(mEtAddr.getText().toString()), Integer.parseInt(mEtAddr.getText().toString()), isOutGoods ? 1 : 0);
                mParentModel = parseObject(json, EVModel.class);
                mModel = mParentModel.getEV_json();
                if (mModel.getIs_success() == 1) {
                    if (mModel.getResult() == 0) {
                        showToast("格子柜出货成功");
                        break;
                    }
                }
                showToast("格子柜出货失败");
                break;
        }
        if (json != null) {
            mTvResult.setText(json);
        }
        if (mModel != null) {
            mTvResult.append("\nresult==> " + mModel.getResult());
        }
    }

    /**
     * 转换类型
     * @param json
     * @param evModelClass
     * @return
     */
    private EVModel parseObject(String json, Class<EVModel> evModelClass) {
        Gson gson = new Gson();
        return gson.fromJson(json, evModelClass);
    }

    /**
     * 检查端口号
     */
    private void checkPortId() {
        if (port_id <= 0) {
            showToast("请先点击注册按钮");
        }
        return;
    }

    /**
     * 检查端口号
     */
    private void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
